package io.github.idol

import android.app.Application
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import io.github.idol.data.ApiRepository
import io.github.idol.login.LoginViewModel
import io.github.idol.roomlist.RoomListViewModel
import io.github.idol.roomlist.room.RoomDetailViewModel

@Suppress("UNCHECKED_CAST")
class ViewModelFactory(
        private val application: Application,
        private val apiRepository: ApiRepository
): ViewModelProvider.NewInstanceFactory(){

    // TODO:how to solve exception situation
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if(modelClass == LoginViewModel::class.java){
            return LoginViewModel(application,apiRepository) as T
        }else if(modelClass == RoomListViewModel::class.java){
            return RoomListViewModel(application,apiRepository) as T
        }else if(modelClass == RoomDetailViewModel::class.java){
            return RoomDetailViewModel(application,apiRepository) as T
        }else{
            return null as T
        }
    }
}